<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="ROS杂篇 ROS中使用Kinect V2 Part1：安装+使用, python,machine learning,deep learning,html,css,c,c++,cpp,cmake,ros,linux,ubuntu">
    <meta name="description" content="Kinect是非常流行的获取深度、彩色、点云的相机。本文介绍了Kinect相机的具体性能，以及如何在ROS调用Kinect相机。">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>ROS杂篇 ROS中使用Kinect V2 Part1：安装+使用 | JackWang&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 5.4.2">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>



   <style>
    body{
       background-image: url(https://cdn.jsdelivr.net/gh/Tokisaki-Galaxy/res/site/medias/background.jpg);
       background-repeat:no-repeat;
       background-size: 100% 100%;
       background-attachment:fixed;
    }
</style>



<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">JackWang&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-book-reader" style="zoom: 0.6;"></i>
      
      <span>博客</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按标签归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按目录归类文章</span>
        </a>
      </li>
      
      <li>
        <a href="/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
	  <span>按日期分类文章</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>



<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">JackWang&#39;s Blog</div>
        <div class="logo-desc">
            
            JackWang的个人博客
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book-reader"></i>
			
			博客
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
                              <span>按标签归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
                              <span>按目录归类文章</    span>

                  </a>
                </li>
              
                <li>

                  <a href="/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
                              <span>按日期分类文章</    span>

                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('抱歉，这篇文章并不想让所有人都看到，请输入授权密码观看')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118214830894.png')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">ROS杂篇 ROS中使用Kinect V2 Part1：安装+使用</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Ubuntu/">
                                <span class="chip bg-color">Ubuntu</span>
                            </a>
                        
                            <a href="/tags/ROS/">
                                <span class="chip bg-color">ROS</span>
                            </a>
                        
                            <a href="/tags/melodic/">
                                <span class="chip bg-color">melodic</span>
                            </a>
                        
                            <a href="/tags/Kinect-V2/">
                                <span class="chip bg-color">Kinect V2</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/ROS%E6%9D%82%E7%AF%87/" class="post-category">
                                ROS杂篇
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2021-11-18
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2023-06-01
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    5.9k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    22 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <blockquote>
<p>Kinect是非常流行的获取深度、彩色、点云的相机。本文介绍了Kinect相机的具体性能，以及如何在ROS调用Kinect相机。</p>
</blockquote>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118214830894.png" alt="最终效果图"></p>
<span id="more"></span>
<h1 id="ROS杂篇-ROS中使用Kinect-V2-Part1：安装-使用"><a href="#ROS杂篇-ROS中使用Kinect-V2-Part1：安装-使用" class="headerlink" title="ROS杂篇 ROS中使用Kinect V2 Part1：安装+使用"></a>ROS杂篇 ROS中使用Kinect V2 Part1：安装+使用</h1><blockquote>
<p>前言：为什么要写这篇博客</p>
</blockquote>
<p>由于我目前的工作需要我编写服务机器人的代码，而服务机器人就免不了需要视觉信息来指导机器人完成服务。包括RGB彩色相机获得的彩色图像、深度相机获得的深度图像等等各种视觉传感器。对于简单的RGB相机拍摄到的RGB彩色图像，由于全球统一的接口，因此有很多工具都可以帮助我们从<code>/dev/video*</code>中读取到彩色图像。通过OpenCV的<code>VideoCapture</code>就可以读到。然而问题的关键在于，针对一些特定的设备，例如深度相机，目前全球尚无统一的标准，因此在数据流中图像的格式各个厂家都是不一样的。因此这个时候想要读取到这些特殊相机的图像就需要花一番功夫了。尤其是在硬件厂商不提供Linux的驱动的时候更难受。</p>
<p>由于我具体的开发平台配置就是 <code>Ubuntu 18.04</code> + <code>ROS Melodic</code> + <code>Kinect V2</code>相机。<strong>所以特地写这篇博客记录一下ROS中如何配置Kinect V2相机</strong></p>
<p><strong>下文针对Ubutnu 18.04 + ROS Melodic 验证可行</strong></p>
<h2 id="1-什么是-Kinect-相机？"><a href="#1-什么是-Kinect-相机？" class="headerlink" title="1. 什么是 Kinect 相机？"></a>1. 什么是 Kinect 相机？</h2><blockquote>
<p>正如我一向的态度，学什么、做什么前先问问为什么要学/做，再问问自己学/做的是什么。为什么要做上面已经回答了，因为我需要用它。那接下来就是要做的是什么？</p>
</blockquote>
<p>个人的角度来说，<code>Kinect</code> 相机就是一款特殊的相机，他可以读取到RGB彩色图像和深度图像，通过<code>Kinect</code>相机采集到的图像和深度信息，我们就能够快乐的进行开发~</p>
<p>虽然<code>Kinect V2</code>在2017年由于<code>Xbox</code>取消了<code>Kinect</code>的接口而停产，但是发现到<code>Kinect</code>相机价值的微软随后推出的 <code>Kinect V3</code> 和 <code>Kinect DK</code> 都是专门为科研推出的设备。（我有幸用过 <code>Kinect DK</code>，上面部署了现成的 <code>Human Motion Estimation</code> 的模型，可以直接获得人体的关键点，非常好用）</p>
<blockquote>
<p>摘自<code>维基百科</code></p>
<p><strong>Kinect</strong>是由<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/微軟">微软</a>开发，应用于<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/Xbox_360">Xbox 360</a>和<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/Xbox_One">Xbox One</a>主机的周边设备。它让玩家不需要手持或踩踏<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/控制器">控制器</a>，而是使用<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/语音指令">语音指令</a>或<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/手势">手势</a>来操作Xbox 360和Xbox One的系统界面。它也能捕捉玩家全身上下的动作，用身体来进行游戏，带给玩家“免控制器的游戏与娱乐体验”。此设备是<a target="_blank" rel="noopener" href="https://zh.wikipedia.org/wiki/微軟研究院">微软研究院</a>的研究成果之一。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118142428272.png" alt="维基百科上对Kinect相机的介绍"></p>
</blockquote>
<p>可以看得出来，<code>Kinect</code>相机最初其实是由Windows为<code>Xbox</code>游戏机准备的外设，通过Kinect相机可以玩一些体感游戏。不过多好的设备当然是要我们来折腾的，拿来单纯玩游戏就有点太奢侈了。</p>
<p>不过正式用于Kinect相机是微软家的产品，按照微软的尿性大概是不会给开源驱动的（虽然这几年微软逐渐在拥抱开源，可是KinectV1、V2都是2010年的产品啊），事实也的确如此。Kinect目前微软官方的驱动只有在Windows上才有。这也就是为什么我们稍后等下需要安装<code>Kinect V2</code>的驱动。</p>
<h2 id="2-Kinect-相机介绍"><a href="#2-Kinect-相机介绍" class="headerlink" title="2. Kinect 相机介绍"></a>2. Kinect 相机介绍</h2><blockquote>
<p>以下内容参考博客：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/traceplus/p/4136297.html">https://www.cnblogs.com/traceplus/p/4136297.html</a></p>
</blockquote>
<h3 id="A-Kinect-V1"><a href="#A-Kinect-V1" class="headerlink" title="A. Kinect V1"></a>A. Kinect V1</h3><p>2012年美国微软发售的<code>Kinect V1</code>，因为可以很方便就能取得Depth（深度）和 skeleton（人物姿势）等信息，被全世界的开发者和研究人员关注。</p>
<p><code>Kinect V1</code>的Depth传感器，采用了<code>Light Coding</code>的方式，读取投射的红外线的<code>pattern</code>，通过<code>pattern</code>的变形来取得Depth的信息。为此，Depth传感器分为投射红外线的IR Projector（左）和读取的IR Camera（右）。此外还有<code>Kinect V1</code>中间还搭载了RGB相机。</p>
<p><code>Light Coding</code>是以色列的<code>PrimeSense</code>公司的Depth传感器技术，于2013年被美国苹果公司收购。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/012317367015237.jpg" alt="Kinect V1的图片"></p>
<h3 id="B-Kinect-V2"><a href="#B-Kinect-V2" class="headerlink" title="B. Kinect V2"></a>B. Kinect V2</h3><p><code>Kinect V2</code>获得Depth信息采用的则是<code>Time of Flight(TOF)</code>的方式，通过从投射的红外线反射后返回的时间来取得Depth信息。红外发射器和接收器在面板底部，因此看不到外观，不过<code>Color Camera</code>旁边是红外线<code>Camera</code>(左)和投射脉冲变调红外线的<code>Porjector</code>（右）。</p>
<p>微软过去收购过基于<code>TOF</code>方式的深度传感器技术的公司（注：应该是指的3DV），已经在使用这个技术，不过没有详细的公布。</p>
<p>（额外插一句，TOF是目前大多数点云数据获取的方式）</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/012317384052450.jpg" alt="Kinect V2"></p>
<h3 id="C-两者对比"><a href="#C-两者对比" class="headerlink" title="C.两者对比"></a>C.两者对比</h3><p><strong>首先是两者的参数</strong></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118144936589.png" alt="Kinect V1 和 V2 性能对比"></p>
<p><strong>然后需要注意的是两者的接口</strong></p>
<ul>
<li>V1的要求是USB2.0理论传输速率是60MB/s，v2是USB3.0理论传输速率是500MB/s。</li>
<li>对于<code>Kinect V1</code>，对XRGB四通道图像，30fps的帧率下每秒所需传输的数据大小为640 x 480 x 4 x 30约为35M；再加上USHORT格式的深度图，30fps，每秒传输数据量为320 x 240 x 2 x 30约为4M。总计约为40MB/s，因为带宽有限，所以在保证画面帧率稳定的情况下，分辨率只能如此，而且基本上必须独占一个USB接口。</li>
<li>对于<code>Kinect V2</code>的情况，彩色图像1920 x 1080 x 4 x 30 约为237M，深度图像512 x 424 x 2 x 30约为12M，总计约为250M/s。所以非USB3.0不可，否则传输不了这么大的数据量。</li>
</ul>
<h2 id="3-安装Kinect驱动-ROS中间件"><a href="#3-安装Kinect驱动-ROS中间件" class="headerlink" title="3. 安装Kinect驱动+ROS中间件"></a>3. 安装Kinect驱动+ROS中间件</h2><h3 id="A-概述"><a href="#A-概述" class="headerlink" title="A. 概述"></a>A. 概述</h3><p>首先需要说明的是开发的方式，因为Kinect相机是物理硬件，所以获取到的是二进制数据流，我们首先需要使用驱动将二进制数据流转换为有意义的图像，然后在我们自己的程序针在这些图像的基础上去进行开发。</p>
<ul>
<li><strong>将二进制数据流转换为图像的程序对应下图的Kinect Driver</strong></li>
<li><strong>我们自己的程序对应下图的<code>Application</code></strong></li>
<li><strong>需要注意的是，在ROS中通常只会有一个程序调用Kinect Driver，因此不考虑右边的工作模型。右边的模型只有多人、多任务同时要调用Kinect</strong> <strong>Driver时候才会有用，针对单任务其实没有啥影响</strong></li>
</ul>
<p>在<code>Kinect</code>标准的<code>Windows</code>上，<code>Kinect</code>的驱动是Windows上直接下载的，然后<code>Windows</code>还提供了<code>Kinect</code>的SDK，里面有现成的人体骨架识别的<code>API</code>，所以可以直接用。<strong>但是在Linux的ROS上想要使用Kinect相机的话，把二进制数据流转换为图像的驱动需要我们自己写，然后SDK+程序得自己开发</strong>。</p>
<ul>
<li>幸运的是，已经有人写好了<code>Kinect</code>的驱动，因此我们直接下载驱动即可。但是<code>Kinect</code>驱动只能帮助我们能在<code>Ubuntu</code>上获得Kinect的图像，所以接下来我们需要做的第二件事就是编写一个<code>ROS</code>调用<code>Kinect</code>的节点来调用<code>Kinect</code>驱动，并将获得的图像以话题的形式发布到<code>ROS</code>中去。</li>
<li>更加幸运的是，这一步也已经有人帮我们做好了，我们需要需要做的下载、编译这个调用Kinect驱动获得图像、并以<code>ROS</code>话题形式发布的<code>ROS</code>功能包（相当于中间件）。</li>
</ul>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/012317438589516.png" alt="开发方式"></p>
<p>因此，整个安装流程分成两步，</p>
<ol>
<li>安装 <code>Kinect</code> 的驱动</li>
<li>安装<code>ROS</code>功能包</li>
</ol>
<h3 id="B-安装Kinect-V2驱动——libfreenect2"><a href="#B-安装Kinect-V2驱动——libfreenect2" class="headerlink" title="B. 安装Kinect V2驱动——libfreenect2"></a>B. 安装Kinect V2驱动——libfreenect2</h3><p>首先需要安装<code>Kinect</code>在<code>Ubuntu</code>上的驱动：<code>libfreenect2</code>。</p>
<p>这个驱动是<code>Github</code>上开源的用<code>C++</code>和<code>Cmake</code>写的<code>Kinect V2</code>的驱动，所以我们稍后克隆下来之后安装一下依赖，然后配置<code>CMake</code>生成<code>Makefile</code>、然后make编译，最后make install安装库即可。</p>
<h4 id="1-安装依赖"><a href="#1-安装依赖" class="headerlink" title="1. 安装依赖"></a>1. 安装依赖</h4><p>实现用apt包管理器安装下稍后编译时候的依赖</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project$ <span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> build-essential cmake pkg-config libturbojpeg libjpeg-turbo8-dev mesa-common-dev freeglut3-dev libxrandr-dev libxi-dev libglfw3-dev libglfw3-dev libopenni2-dev libusb-dev libturbojpeg0-dev
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>由于我已经安装过了，所以这一步我不会安装任何东西。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118172852901.png" alt="安装依赖"></p>
<h4 id="2-下载源码"><a href="#2-下载源码" class="headerlink" title="2. 下载源码"></a>2. 下载源码</h4><p><code>git</code>直接下载源码即可，如果下载的慢的话，要么科学上网，要么先用国内的码云gitee克隆一下，然后再下载码云的仓库。关于如何使用码云加速<code>github</code>下载可以参考我的这篇文章（挖个坑，以后补）</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project$ <span class="token function">git</span> clone https://github.com/OpenKinect/libfreenect2.git
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118173143020.png" alt="下载源码"></p>
<p>下载之后的项目文件结构，可以看出来，是一个非常经典的<code>CMake</code>的项目</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118173557080.png" alt="下载后的项目结构"></p>
<h4 id="3-配置-amp-编译"><a href="#3-配置-amp-编译" class="headerlink" title="3. 配置&amp;编译"></a>3. 配置&amp;编译</h4><p>对于一个<code>CMake</code>工程，得到项目最后的成果，即最终的动态链接库/共享库、可执行文件一共需要两步。第一步是配置（<code>Config</code>），第二步是构建（<code>Build</code>）。配置指的是设置项目的一些配置，比如说编译器使用的语法标准、是否禁止编译器在编译阶段优化代码等等。这里不展开讲了，具体内容可以参考我的<code>CMake</code>系列文章（挖个坑）。</p>
<p>首先创建一个编译空间</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project$ <span class="token function">cd</span> libfreenect2/
<span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2$ <span class="token function">mkdir</span> build
<span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2$ <span class="token function">cd</span> build/
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>接下来再使用<code>CMake</code>针对<code>build</code>文件夹外的<code>CMakeLists</code>进行配置，需要注意的是，由于我已经配好了<code>Nvidia</code>的显卡驱动和<code>CUDA</code>，因此我指定使用<code>CUDA</code>编译，这样运行的时候就会有加速。此外配置的时候要指定安装到的位置，这个安装到的位置稍后安装ROS功能包的时候会用到，所以不建议修改，用默认的就行。</p>
<p>具体来说我们都是在命令行使用-D参数来指定<code>CMake</code>的宏及其值(D=define)，<code>CMakeLists</code>中会检查这些宏的值。如果你不是<code>CUDA</code>的话就把后面指定<code>CUDA</code>的宏删掉即可</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2/build$ cmake <span class="token punctuation">..</span> -DCMAKE_INSTALL_PREFIX<span class="token operator">=</span><span class="token variable">$HOME</span>/freenect2 -DENABLE_CXX11<span class="token operator">=</span>ON -DCUDA_PROPAGATE_HOST_FLAGS<span class="token operator">=</span>off
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118184945046.png" alt="CMake配置工程"></p>
<p>可以看到<code>CMake</code>配置的流程还是很清晰的，首先会去寻找系统中的编译器版本然后再去检查当前编译器是否至此指定版本的语法，接下来去寻找包等等。全部配置过程没有问题的话就会显示最后的三句话，当前<code>build</code>编译空间下也多出来很多中间文件。<code>Makefile</code>就是稍后<code>make</code>解析的对象。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2/build$ <span class="token function">ls</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118185449344.png" alt="配置的结果"></p>
<p>然后我们make编译即可</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2/build$ <span class="token function">make</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>我这里的编译可以说是顺风顺水，主要原因其实是我已经配置过一遍了，这是写教程时候删掉了全部的文件重头再来的结果。<strong>其实在第一次配的时候在编译这里遇到了两三个问题</strong>，不过我都解决掉了，现在没有办法把这些问题复现出来。这里挖个坑，下一次装机补上。主要的两个问题是第一次会报错不存在一个叫<code>libGL.so</code>的共享库，第二次则是在最后编译到100%之后链接的时候报错说没有定义的引用：<code>undefined reference to _glapi_tls_Current</code>。</p>
<p>其实这两个问题都和<code>OpenGL</code>这个图形库有关（<code>Graphic Librar</code>y）。图形负责渲染材质等等，是将数据显示到我们的显示器上的库，包括看到的桌面、窗口等等都是用图形库渲染出来的。而<code>Kinect</code>捕获到的数据要转成图片显示，就必然会用到<code>OpenGL</code>这个库。第一个报错是因为管道断开（<code>libGL.so</code>是一个软连接），重新链接就行，第二个报错则是因为<code>libGL.so</code>链接到的共享库版本太老，指定一个更新的即可。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118185715177.png" alt="编译的过程"></p>
<p>最后，由于<code>libfreenect2</code>最终生成的是一个驱动，因此他最终生成的结果不仅有可执行文件，还有共享库，我们需要<code>make install</code>来安装一下共享库。说是安装，其实就是把共享库复制到指定的位置去，一般的项目都是把共享库复制到系统链接时候的搜索路径，这样编译的时候系统会自动的去搜索。<strong>不过由于我们上面CMake在配置的时候指定了安装位置，因此他其实会安装到我们的家目录下面</strong></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2/build$ <span class="token function">make</span> <span class="token function">install</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>从命令的输出确实能够看到安装的位置就是我们的家目录</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118190908729.png" alt="安装的结果"></p>
<h4 id="4-设置USB规则"><a href="#4-设置USB规则" class="headerlink" title="4. 设置USB规则"></a>4. 设置USB规则</h4><p>由于<code>libfreenect2</code>是一个驱动，因此他需要和底层的硬件做交互，所以需要设置一下<code>USB</code>规则。在<code>Linux</code>中所有的串口都是由一个叫<code>udev</code>的程序负责的，他相当于<code>Windows</code>的设备管理器。这里挖个坑，以后补上关于<code>udev</code>的内容。更多细节可以参考<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/dd6cecd7755a">这篇简书的博客</a></p>
<blockquote>
<p><strong>以下内容引用自简书博客</strong></p>
<p><strong>udev在linux的那个位置</strong></p>
<p>udev的守护进程在linux的位置在systemd中的位置如下所示，举个例子：如果向pc中插入一个usb设备，kernel在总线上发现这个设备，使用dirver初始化，在sysfs创建device目录等操作之后，将通知用户空间的udev，然后上层的显示层才能看到这个usb设备，并最终将它显示在desktop上：</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/330043-6ecf51d6a519b4fe.png" alt="Systemd守护进程所处的位置"></p>
</blockquote>
<p>我们直接把规则文件复制到<code>udev</code>的配置文件夹下即可</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token function">sudo</span> <span class="token function">cp</span> <span class="token punctuation">..</span>/platform/linux/udev/90-kinect2.rules /etc/udev/rules.d/
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h4 id="5-验证安装"><a href="#5-验证安装" class="headerlink" title="5. 验证安装"></a>5. 验证安装</h4><p>最后上面的步骤一步步做下来，应该是没有问题的，不过在编译驱动的时候我们观察到make最后在生成一个叫做<code>Protonect</code>的可执行文件。这个程序其实就是验证安装的程序。我们接上<code>Kinect</code>的连接线，然后运行这个程序，如果安装成功就能够看到下面的图片</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/libfreenect2/build$ ./bin/Protonect
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>四张图中左上角是深度图，左下角是RGB图像，右下角是点云的图像，右上角是带有RGB的点云的图像。需要注意的是，点云的图像是通过深度图推算出来的。深度图只有一个Depth通道，数值越大表示越远，看起来就越黑。而点云图是XYZ三个通道，分别表示点相对相机的的XYZ坐标。而右上角的点云则是RGBXYZ六个通道图片。右边看到，有<code>CUDA</code>加速喧嚷延迟基本都在10ms左右，速度还是非常快的</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118193300100.png" alt="验证程序的运行结果"></p>
<h4 id="6-安装驱动可能遇到的问题"><a href="#6-安装驱动可能遇到的问题" class="headerlink" title="6. 安装驱动可能遇到的问题"></a>6. 安装驱动可能遇到的问题</h4><p>暂时没有，留待下次重装系统时候补充。</p>
<h3 id="C-安装ROS-Kinect深度相机功能包——iai-kinect2"><a href="#C-安装ROS-Kinect深度相机功能包——iai-kinect2" class="headerlink" title="C. 安装ROS Kinect深度相机功能包——iai_kinect2"></a>C. 安装ROS Kinect深度相机功能包——iai_kinect2</h3><p>就像在<code>Melodic</code>上使用<code>CV_Bridge</code>一样（挖个<code>Melodic</code>编译<code>CV_Bridge</code>的坑，以后补上），<code>iai_kinect2</code>也需要我们从<code>github</code>上下载源码然后编译。</p>
<p>在编译之后，我们想要通过Kinect获得深度图像、点云图像等等直接启动这个<code>iai_kinect2</code>中的节点就行。这个节点将会调用libfreenect，并将获取到的图像发布到指定的话题里，因此我们订阅指定的话题即可得到数据。</p>
<h4 id="1-下载并编译功能包"><a href="#1-下载并编译功能包" class="headerlink" title="1. 下载并编译功能包"></a>1. 下载并编译功能包</h4><p>由于iai-<code>kinect2</code>本质上是<code>ROS</code>的一个功能包，而<code>ROS</code>的功能包的发布都是以功能包的形式发布的，因此我们只需要把这个功能包下载到工作空间中并进行编译即可。为了不污染我们自己的工作空间，避免每次都要编译这个包，我们新创建一个叫做<code>install_iai_kinect</code>的工作空间的安装这个包</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project$ <span class="token function">mkdir</span> install_iai_kinect2
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project$ <span class="token function">cd</span> install_iai_kinect2/
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2$ <span class="token function">mkdir</span> src
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2$ <span class="token function">cd</span> src/
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2/src$ catkin_init_workspace 
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2/src$ <span class="token function">cd</span> <span class="token punctuation">..</span>
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2$ catkin_make
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2$ ll
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118205323865.png" alt="初始化之后的工作空间"></p>
<p>然后git下载功能并安装依赖，注意这里如果前面改了<code>libfreenect</code>的安装路径的话，这里编译的时候要添加一个<code>CMake</code>的宏来指定<code>libfreenect</code>的路径，参考<a target="_blank" rel="noopener" href="https://github.com/code-iai/iai_kinect2#install">文章</a>。</p>
<p>此外，在使用<code>rosdep</code>安装的时候会遇到一个“报错”（见下），这个其实是正常的，因为<code>kinect2_brideg</code>、<code>kinect2_cailbration</code>、<code>kinect2_caliration</code>、<code>kinect2_viewer</code>这四个节点被<code>rosdep</code>认为是功能包了，但他们其实都只是<code>iai_kinect2</code>的一部分。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2$ <span class="token function">cd</span> src/
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2/src$ <span class="token function">git</span> clone https://github.com/code-iai/iai_kinect2.git
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2/src$ <span class="token function">cd</span> iai_kinect2
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2/src/iai_kinect2$ rosdep <span class="token function">install</span> -r --from-paths <span class="token keyword">.</span>
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2/src/iai_kinect2$ <span class="token function">cd</span> <span class="token punctuation">..</span>/<span class="token punctuation">..</span>/
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai_kinect2$ catkin_make -DCMAKE_BUILD_TYPE<span class="token operator">=</span><span class="token string">"Release"</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p>ERROR: the following packages/stacks could not have their rosdepc keys resolved<br>to system dependencies:<br>kinect2_bridge: Cannot locate rosdep definition for [kinect2_registration]<br>kinect2_calibration: Cannot locate rosdep definition for [kinect2_bridge]<br>kinect2_viewer: Cannot locate rosdep definition for [kinect2_bridge]<br>iai_kinect2: Cannot locate rosdep definition for [kinect2_registration]</p>
</blockquote>
<p>最后没有error就成功安装功能包了，但是不要忘记<code>source</code>一下，因为这个功能包不在<code>ROS</code>的元功能包中。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118205948236.png" alt="编译的结果"></p>
<h4 id="2-验证安装"><a href="#2-验证安装" class="headerlink" title="2. 验证安装"></a>2. 验证安装</h4><p>同样，经历过上面的步骤之后，我们脸上<code>Kinect V2</code>的连接线来跑跑节点，看看有没有问题。类似于<code>cv_bridge</code>，<code>iai-kinect2</code>编译之后会得到一个<code>kinect_bridge</code>，我们利用其中的launch文件来测试一下。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai-kinect$ <span class="token function">source</span> devel/setup.bash 
<span class="token punctuation">(</span>ros<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai-kinect$ roslaunch kinect2_bridge kinect2_bridge.launch
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118204334851.png" alt="launch启动整个kinect_bridge"></p>
<p>然后我们再开一个终端来看看节点图</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai-kinect$ rqt_graph
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>看起来还不错，可以看到<code>kinect_bridge</code>其实只是一个交互的节点，他的后端数据处理都是依靠的其他的节点，<code>kinect2</code>负责调用<code>libfreenect</code>驱动，剩下的hd、sd、qhd分别负责处理高中低分辨率的图像。</p>
<p>不过由于rqt是通过rosmaster中的注册信息来画节点图的，因此没有被订阅的话题无法被显示出来，我们看看所有的话题</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118203224904.png" alt="Kinect_Bridge节点图"></p>
<p>新开一个终端</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~$ rostopic list
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>可以看到有很多的话题，不过其实所有的话题分成三组，<code>hd</code>表示高分辨率，<code>qhd</code>表示中分辨率，<code>sd</code>表示低分辨率</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118203435667.png" alt="Kinect_Bridge发布的所有的画图"></p>
<p>最后其实<code>kinect_bridge</code>提供了可视化的节点，不过首先需要对kinect相机进行校准，否则是有偏差的，不过我们这里只是为了验证，因此跑起来看看</p>
<pre class="line-numbers language-bash"><code class="language-bash">rosrun kinect2_viewer kinect2_viewer kinect2 sd cloud
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>看起来效果还不错，不过由于订阅的是低分辨的图片，因此图片放大之后实际上像素间就会出现一些缺失的黑色点</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118203934128.png" alt="可视化的结果"></p>
<h4 id="3-安装时候的一些说明"><a href="#3-安装时候的一些说明" class="headerlink" title="3. 安装时候的一些说明"></a>3. 安装时候的一些说明</h4><blockquote>
<h4 id="1-编译的时候弹出来警告"><a href="#1-编译的时候弹出来警告" class="headerlink" title="1. 编译的时候弹出来警告"></a>1. 编译的时候弹出来警告</h4><p>具体的警告如下图。只是警告而已，不是错误，不用管。因为<code>catkin</code>作为<code>cmake</code>的上级编译系统，最后还是调用的<code>g++</code>，<code>g++</code>在编译的是帮我们检查代码，警告我们哪里语法不规范啥的，没必要管，能用就行。</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118202235817.png" alt="编译时候的警告"></p>
</blockquote>
<h4 id="4-安装功能包可能会遇到的问题"><a href="#4-安装功能包可能会遇到的问题" class="headerlink" title="4. 安装功能包可能会遇到的问题"></a>4. 安装功能包可能会遇到的问题</h4><blockquote>
<h4 id="1-CMake-Error-at-usr-lib-x86-64-linux-gnu-cmake-Qt5Gui-Qt5GuiConfig-cmake-27-message-：Qt5的报错"><a href="#1-CMake-Error-at-usr-lib-x86-64-linux-gnu-cmake-Qt5Gui-Qt5GuiConfig-cmake-27-message-：Qt5的报错" class="headerlink" title="1. CMake Error at /usr/lib/x86_64-linux-gnu/cmake/Qt5Gui/Qt5GuiConfig.cmake:27 (message)：Qt5的报错"></a>1. <strong>CMake Error at /usr/lib/x86_64-linux-gnu/cmake/Qt5Gui/Qt5GuiConfig.cmake:27 (message)</strong>：Qt5的报错</h4><p>具体报错如下图，即缺少了<code>libEGL.so</code>这个共享库</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118200845339.png" alt="Qt5报错"></p>
<p><strong>解决方法：</strong></p>
<p>和上面安装驱动时候遇到的报错差不多，估计是一个尿性，即软连接断了，先去<code>/usr/lib/x86-linux-gnu</code>下看看。果然，红色的字体，断开的链接。即<code>/usr/lib/x86_64-linux-gnu/libEGL.so</code>这个软连接本来指向的文件没了。不过看这个样子有一个1.1.0版本的，估计是某次<code>apt upgrade</code>时候更新了这个库。</p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai-kinect$ ll /usr/lib/x86_64-linux-gnu/libEGL.so*
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118201305342.png" alt="libEGL.so断开的软连接"></p>
<p>那么解决思路就很简单了，我们先删掉原有的断掉的链接，然后让他再指向新版本就行了，毕竟新版本一般都是向后兼容的。除了<code>Python2</code>到<code>Python3</code></p>
<pre class="line-numbers language-bash"><code class="language-bash"><span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai-kinect$ <span class="token function">sudo</span> <span class="token function">rm</span> /usr/lib/x86_64-linux-gnu/libEGL.so 
<span class="token punctuation">(</span>base<span class="token punctuation">)</span> jack@jack-Alienware-m15-R3:~/project/install_iai-kinect$ <span class="token function">sudo</span> <span class="token function">ln</span> -s /usr/lib/x86_64-linux-gnu/libEGL.so.1.1.0 /usr/lib/x86_64-linux-gnu/libEGL.so
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>现在看着就没问题了，管道对了</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118201854870.png" alt="重置libEGL.so的指向"></p>
<p>然后再进行编译即可</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118202028422.png" alt="功能包可以正常编译"></p>
</blockquote>
<h2 id="4-使用kinect-bridge"><a href="#4-使用kinect-bridge" class="headerlink" title="4. 使用kinect_bridge"></a>4. 使用kinect_bridge</h2><p>使用<code>kinect_bridge</code>其实特别简单，就是订阅话题而已，为此我们快速写一个<code>Python</code>脚本测试，注意测试时候新建的功能包的依赖不要加<code>iai_kinect2</code>，因为<code>kinect_bridge</code>实现了低耦合性，所有的消息全部都是<code>ROS</code>中的标准消息类型，因此没有提供任何ROS编译之后可以引用的库。<strong>下面是是一个错误的示例</strong>。（挖个坑，以后出一个关于<code>Vscode</code>开发<code>ROS</code>的环境搭建，真的<code>VScode</code>越用越爽）</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118210715487.png" alt="功能包的依赖"></p>
<p>正因为<code>kinect_bridge</code>中的消息全部都是<code>ROS</code>的标准消息，所有的图像都是<code>sensor_msgs/Image</code>或者<code>sensor_msgs/CompressedImage</code>。而点云则是<code>sensor_msgs/PointCloud2</code>，我们可以非常快速的写一个测试代码，代码见下（<strong>注意如果你要写C++的话不要在iai_bridge的工作空间中编译，会报错，因为Kinect_Bridge没有提供ROS编译、安装相关的CMake指令</strong>）</p>
<p>关于<code>ROS</code>中使用<code>conda</code>、还有<code>cv_bridge</code>、<code>ROS</code>使用<code>Python</code>等问题先挖个坑，后面补上文章</p>
<pre class="line-numbers language-python"><code class="language-python"><span class="token comment" spellcheck="true">#! /home/jack/anaconda3/envs/ros/bin/python</span>

<span class="token triple-quoted-string string">"""rospy 中使用 通过 kinect 获取点云的示例代码 """</span>

<span class="token triple-quoted-string string">"""
@author: Jack Wang
@copyright: Jack Wang
@time: 2021-11-18
"""</span>

<span class="token keyword">import</span> sys
<span class="token keyword">import</span> datetime
<span class="token keyword">import</span> time

<span class="token keyword">import</span> cv2
<span class="token keyword">from</span> colorama <span class="token keyword">import</span> Fore<span class="token punctuation">,</span> Style

<span class="token comment" spellcheck="true"># 我在 Python3 下编译了cv_bridge，所以要避免import了下载ROS时候一并下载的系统的Python2.7的cv_bridge</span>
<span class="token keyword">for</span> path_idx <span class="token keyword">in</span> range<span class="token punctuation">(</span>len<span class="token punctuation">(</span>sys<span class="token punctuation">.</span>path<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">if</span> <span class="token string">"2.7"</span> <span class="token keyword">in</span> sys<span class="token punctuation">.</span>path<span class="token punctuation">[</span>path_idx<span class="token punctuation">]</span><span class="token punctuation">:</span>
        temp <span class="token operator">=</span> sys<span class="token punctuation">.</span>path<span class="token punctuation">.</span>pop<span class="token punctuation">(</span>path_idx<span class="token punctuation">)</span>
        <span class="token keyword">break</span>
sys<span class="token punctuation">.</span>path<span class="token punctuation">.</span>append<span class="token punctuation">(</span>temp<span class="token punctuation">)</span>

<span class="token keyword">import</span> rospy
<span class="token keyword">from</span> sensor_msgs<span class="token punctuation">.</span>msg <span class="token keyword">import</span> Image
<span class="token keyword">from</span> cv_bridge <span class="token keyword">import</span> CvBridge


<span class="token keyword">class</span> <span class="token class-name">MyKinectViewer</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">:</span>
    bridge <span class="token operator">=</span> CvBridge<span class="token punctuation">(</span><span class="token punctuation">)</span>
    today<span class="token punctuation">:</span> str <span class="token operator">=</span> datetime<span class="token punctuation">.</span>date<span class="token punctuation">.</span>today<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__str__<span class="token punctuation">(</span><span class="token punctuation">)</span>
    start_sec<span class="token punctuation">:</span> float <span class="token operator">=</span> time<span class="token punctuation">.</span>time<span class="token punctuation">(</span><span class="token punctuation">)</span>
    font <span class="token operator">=</span> cv2<span class="token punctuation">.</span>FONT_HERSHEY_DUPLEX
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span>
        super<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        rospy<span class="token punctuation">.</span>loginfo<span class="token punctuation">(</span>msg<span class="token operator">=</span>f<span class="token string">"{Fore.GREEN}启动KinectBridge订阅节点！{Style.RESET_ALL}"</span><span class="token punctuation">)</span>
        subcriber <span class="token operator">=</span> rospy<span class="token punctuation">.</span>Subscriber<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"/kinect2/sd/image_color_rect"</span><span class="token punctuation">,</span> data_class<span class="token operator">=</span>Image<span class="token punctuation">,</span> callback<span class="token operator">=</span>self<span class="token punctuation">.</span>image_cb<span class="token punctuation">,</span> queue_size<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">image_cb</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> msg<span class="token punctuation">:</span> Image<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> None<span class="token punctuation">:</span>
        cv_img <span class="token operator">=</span> self<span class="token punctuation">.</span>bridge<span class="token punctuation">.</span>imgmsg_to_cv2<span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
        cv_img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>putText<span class="token punctuation">(</span>cv_img<span class="token punctuation">,</span> f<span class="token string">"{self.today}, start times: {time.time()-self.start_sec:>.2f}"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">380</span><span class="token punctuation">)</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>font<span class="token punctuation">,</span> <span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token punctuation">)</span>
        cv_img <span class="token operator">=</span> cv2<span class="token punctuation">.</span>putText<span class="token punctuation">(</span>cv_img<span class="token punctuation">,</span> f<span class="token string">"-- by Jack Wang"</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">270</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">)</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>font<span class="token punctuation">,</span> <span class="token number">0.8</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span><span class="token punctuation">)</span>
        cv2<span class="token punctuation">.</span>imshow<span class="token punctuation">(</span><span class="token string">"kinect_test"</span><span class="token punctuation">,</span> cv_img<span class="token punctuation">)</span>
        <span class="token keyword">if</span> cv2<span class="token punctuation">.</span>waitKey<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">27</span><span class="token punctuation">:</span>
            rospy<span class="token punctuation">.</span>loginfo<span class="token punctuation">(</span>f<span class="token string">"{Fore.GREEN}关闭节点{Style.RESET_ALL}"</span><span class="token punctuation">)</span>
            rospy<span class="token punctuation">.</span>signal_shutdown<span class="token punctuation">(</span>reason<span class="token operator">=</span><span class="token string">"pressed esc"</span><span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">"__main__"</span><span class="token punctuation">:</span>
    rospy<span class="token punctuation">.</span>init_node<span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">"kinect_test"</span><span class="token punctuation">,</span> anonymous<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span>
    mkv <span class="token operator">=</span> MyKinectViewer<span class="token punctuation">(</span><span class="token punctuation">)</span>
    rospy<span class="token punctuation">.</span>spin<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>可以看出来，效果还不错</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118214830894.png" alt="节点效果图"></p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118214855870.png" alt="按下ESC键退出节点"></p>
<p>结合我之前写的<code>YOLO V5</code>的<code>ROS</code>图像处理节点，最后可以实现的效果（<code>Yolo V5</code>的<code>ROS</code>节点代码后面会发出来）</p>
<p><img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211118220436969.png" alt="image-20211118220436969"></p>
<p>至此，本文全部结束，我们首先介绍了什么是<code>Kinect</code>相机；然后就<code>Kinect</code>相机的参数、工作原理进行了介绍；接下来讲解了如何在<code>Linux(Ubuntu)</code>上安装<code>Kinect V2</code>相机的驱动：<code>libfreenect2</code>和安装<code>ROS</code>中的<code>Kinect</code>相机调用节点；在最后我们给出了一个简单的小例子，来演示如何使用<code>iai-kinect2</code>功能包得到的图像数据。</p>
<p>关于iai-kinect2的更多使用教程，请看下一篇文章：<code>ROS</code>中<code>iai-kinect2</code>功能包的使用。</p>
<p>6200字，码字不易~，欢迎打赏~，一起推动开源事业进步</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Jack Wang</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://jackwang0107.github.io/2021/11/18/ros-za-pian-an-zhuang-kinect-v2-qu-dong/">https://jackwang0107.github.io/2021/11/18/ros-za-pian-an-zhuang-kinect-v2-qu-dong/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Jack Wang</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Ubuntu/">
                                    <span class="chip bg-color">Ubuntu</span>
                                </a>
                            
                                <a href="/tags/ROS/">
                                    <span class="chip bg-color">ROS</span>
                                </a>
                            
                                <a href="/tags/melodic/">
                                    <span class="chip bg-color">melodic</span>
                                </a>
                            
                                <a href="/tags/Kinect-V2/">
                                    <span class="chip bg-color">Kinect V2</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2021/11/19/ros-za-pian-ros-zhong-shi-yong-kinect-v2-iai-kinect-shi-yong/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211121155602208.png" class="responsive-img" alt="ROS杂篇 ROS中使用Kinect V2 Part2：iai-kinect使用">
                        
                        <span class="card-title">ROS杂篇 ROS中使用Kinect V2 Part2：iai-kinect使用</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本篇详细介绍了iai-kinect2功能包，在此基础上通过iai-kinect2获取深度图像，从而进行ROS中的开发。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-11-19
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/ROS%E6%9D%82%E7%AF%87/" class="post-category">
                                    ROS杂篇
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Ubuntu/">
                        <span class="chip bg-color">Ubuntu</span>
                    </a>
                    
                    <a href="/tags/ROS/">
                        <span class="chip bg-color">ROS</span>
                    </a>
                    
                    <a href="/tags/roslaunch/">
                        <span class="chip bg-color">roslaunch</span>
                    </a>
                    
                    <a href="/tags/cv-bridge/">
                        <span class="chip bg-color">cv_bridge</span>
                    </a>
                    
                    <a href="/tags/melodic/">
                        <span class="chip bg-color">melodic</span>
                    </a>
                    
                    <a href="/tags/OpenCv/">
                        <span class="chip bg-color">OpenCv</span>
                    </a>
                    
                    <a href="/tags/sensor-msgs/">
                        <span class="chip bg-color">sensor_msgs</span>
                    </a>
                    
                    <a href="/tags/Kinect-V2/">
                        <span class="chip bg-color">Kinect V2</span>
                    </a>
                    
                    <a href="/tags/iai-kinect2/">
                        <span class="chip bg-color">iai-kinect2</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2021/11/14/ji-yu-hexo-de-ge-ren-ji-zhu-bo-ke-da-jian-part-3-matery-zhu-ti-xia-de-hexo-you-hua-md-md/">
                    <div class="card-image">
                        
                        <img src="https://jack-1307599355.cos.ap-shanghai.myqcloud.com/img/image-20211115010655463.png" class="responsive-img" alt="基于hexo的个人技术博客搭建 —— Part 3 matery主题下的Hexo博客优化.md">
                        
                        <span class="card-title">基于hexo的个人技术博客搭建 —— Part 3 matery主题下的Hexo博客优化.md</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            hexo本身只是提供了一个博客的框架，博客网站的美化和优化还是需要靠自己配置主题。本文讲解如何利用Matery主题对Hexo搭建的博客进行深度优化
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2021-11-14
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Hexo%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" class="post-category">
                                    Hexo博客搭建
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Hexo/">
                        <span class="chip bg-color">Hexo</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: JackWang&#39;s Blog<br />'
            + '文章作者: Jack Wang<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('2'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2023</span>
            
            <a href="/about" target="_blank">Jack Wang</a>
            <!-- |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a> -->
            <!-- |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">603.8k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
                <span id="sitetime"> Loading ...</span>
                <script>
                    var calcSiteTime = function () {
                        var seconds = 1000;
                        var minutes = seconds * 60;
                        var hours = minutes * 60;
                        var days = hours * 24;
                        var years = days * 365;
                        var today = new Date();
                        var startYear = "2021";
                        var startMonth = "11";
                        var startDate = "12";
                        var startHour = "0";
                        var startMinute = "0";
                        var startSecond = "0";
                        var todayYear = today.getFullYear();
                        var todayMonth = today.getMonth() + 1;
                        var todayDate = today.getDate();
                        var todayHour = today.getHours();
                        var todayMinute = today.getMinutes();
                        var todaySecond = today.getSeconds();
                        var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                        var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                        var diff = t2 - t1;
                        var diffYears = Math.floor(diff / years);
                        var diffDays = Math.floor((diff / days) - diffYears * 365);

                        // 区分是否有年份.
                        var language = 'zh-CN';
                        if (startYear === String(todayYear)) {
                            document.getElementById("year").innerHTML = todayYear;
                            var daysTip = 'This site has been running for ' + diffDays + ' days';
                            if (language === 'zh-CN') {
                                daysTip = '本站已运行 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                daysTip = '本站已運行 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = daysTip;
                        } else {
                            document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                            var yearsAndDaysTip = 'This site has been running for ' + diffYears + ' years and '
                                + diffDays + ' days';
                            if (language === 'zh-CN') {
                                yearsAndDaysTip = '本站已运行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            } else if (language === 'zh-HK') {
                                yearsAndDaysTip = '本站已運行 ' + diffYears + ' 年 ' + diffDays + ' 天';
                            }
                            document.getElementById("sitetime").innerHTML = yearsAndDaysTip;
                        }
                    }

                    calcSiteTime();
                </script>
            
            <br>
            
                <span id="icp"><img src="/medias/icp.png"
                                    style="vertical-align: text-bottom;"/>
                <a href="https://beian.miit.gov.cn" target="_blank">陕ICP备2021014294号-1</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/jackwang0108" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2232123545@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2232123545" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2232123545" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

     
        <script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>
        <script src="/libs/others/TencentCaptcha.js"></script>
        <button id="TencentCaptcha" data-appid="xxxxxxxxxx" data-cbfn="callback" type="button" hidden></button>
    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
